package com.pshcp.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;

/**
 * 手机号脱敏
 */
public class PhoneNoJsonSerializer extends JsonSerializer {

    @Override
    public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if(o == null || !(o instanceof String) || o.toString().length() < 11){
            jsonGenerator.writeObject(o);
            return;
        }
        jsonGenerator.writeString(o.toString().replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2"));
    }
}
